/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = test_ora.c                                        */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/10 by songbb                               */
/*******************************************************************/

#include <stdio.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "apmtest_cunit.h"
#include "frcomm.h"
#include "frmem.h"
#include "../../src/module/oracle/config.h"
#include "t_comm/t_ora_charset/t_ora_charset.h"
#include "t_comm/t_ora_comm/t_ora_comm.h"
#include "t_comm/t_ora_util/t_ora_util.h"
#include "t_comm/t_ora_stmt/t_ora_stmt.h"
#include "t_ora_core/t_ora_core.h"
#include "t_tns/t_ora_packet/t_ora_packet.h"
#include "t_tns/t_ora_accept/t_ora_accept.h"
#include "t_tns/t_ora_connect/t_ora_connect.h"
#include "t_tns/t_ora_redirect/t_ora_redirect.h"
#include "t_tns/t_ora_datadescriptor/t_ora_datadescriptor.h"
#include "t_ttc/t_sql/t_ora_LnxLibThin/t_ora_LnxLibThin.h"
#include "t_ttc/t_sql/t_ora_NUMBER/t_ora_NUMBER.h"
#include "t_ttc/t_sql/t_ora_STRING/t_ora_STRING.h"
#include "t_ttc/t_sql/t_ora_TIMESTAMP/t_ora_TIMESTAMP.h"
#include "t_ttc/t_ora_tti_pro/t_ora_tti_pro.h"
#include "t_ttc/t_ora_mar_engine/t_ora_mar_engine.h"
#include <frservice_mng.h>

static Frmemstat_mng g_st_memstat_mng = {0, FR_MEMSTAT_COUNT_MAX};
static pthread_rwlock_t g_memstat_rwlock = PTHREAD_RWLOCK_INITIALIZER;
static Frmem_env g_st_mem_env = {&g_st_memstat_mng, &g_memstat_rwlock};

FR_STATIC Frexp_ref g_st_exp_ref = {NULL, NULL};
FR_STATIC Frservice_ref g_st_service_Ref = {NULL, NULL};
ApmContext g_st_apm_context = {&g_st_exp_ref, NULL, &g_st_service_Ref, NULL, NULL, &g_st_mem_env};
void *g_p_apm_context = NULL;

int main(int argc, char **argv)
{
    if (fr_slab_init(&g_st_mem_env, MODULE_NAME) == NULL)
        return FR_FAIL;

    apmtest_cunit_registry();

    FunType fun_suites[1024];
    memset(fun_suites, 0x00, sizeof(fun_suites));
    int i_suites_len = sizeof(fun_suites) / sizeof(FunType);
    int i_loop = 0;

    // 加载各个测试组件
    // t_comm/t_ora_charset
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__binary_search);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_str_to_utf8);

    // t_comm/t_ora_comm
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__ora_malloc);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_free);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_set_ttc_type_rep);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_get_ttc_type_rep);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_unset_ttc_type_rep);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_set_ttc_type_rep_jdbc);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_push_sess_phase);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_set_last_sess_phase);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_pop_sess_phase);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_is_sess_phase_null);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_reset_sess_phase);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_add_sess_phase);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_copy_sess_phase);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_remaining_last_sess_phase);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_has_svr_compile_time_capability);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_reset_resp_info);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_sess_set_large_sdu_flag);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__ora_new_report_body);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_packet_need_next_packet);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_packet_is_in_fragment);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_is_sql);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_is_sql_select);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_is_sql_dml);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_service_list_add);

    // t_comm/t_ora_util
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__ora_frtime2datetimestr);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_bin_to_hex_string);

    // t_tns/t_ora_packet
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_header_detect);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_header_unpack);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_init_ora_packet);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__ora_pkt_is_fragmented);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__ora_packet_check_fragment);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_packet_prepare_data);

    // t_ttc/t_ora_tti_pro
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__find_driver_charset);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__is_charset_multibyte);

    // t_ttc/t_sql
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_setLength);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_lnxfpr);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_lnxnuc);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_NUMBER_isZero);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_NUMBER_toChar);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_t_ora_STRING_toChar);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_t_ora_TIMESTAMP_toLongLong);

    // t_ora_core
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__ora_private_session_init);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__ora_destroy_session);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__ora_destroy_ora_packet);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_session_destroy);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_detect);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_analyze);

    //t_ora_accept
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_accept_unpack);

    //add_suite_ora_connect_unpack
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_connect_unpack);

    //ora_datadescriptor
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_datadescriptor_unpack);

    //ora_redirect_unpack
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_redirect_unpack);

    // t_comm/t_ora_stmt
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_free_new_stmt);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_set_new_stmt);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_push_ttc7_new_stmt);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_pop_ttc7_new_stmt);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_clear_ttc7_new_stmt);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_get_stmt);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_get_new_stmt);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_get_current_stmt);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_stmt_add_define_metadata);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_stmt_add_bind_metadata);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_stmt_add_column_metadata);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_create_closed_cursor_node);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_add_closed_cursor);

    // t_ttc/t_ora_mar_engine
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_mar_check_bufflen);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_mar_check_bufflen_from);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_mar_skip);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__ora_mar_detect_big_chunks);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__ora_mar_getDataFromBuffer);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__ora_mar_getNBytes);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__ora_mar_getNBytes_by_len);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_mar_unmarshalBuffer);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_mar_get_bytes);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_mar_unmarshalCLR_ttc);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_mar_unmarshalCLR);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_mar_unmarshalCLR_five);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_mar_unmarshalCHR);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_mar_unmarshalDALC);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_mar_unmarshalCLRforREFS);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_mar_unmarshalO2U);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_ora_mar_unmarshalKeywordValue);

    for (; i_loop < i_suites_len; i_loop++)
    {
        if (fun_suites[i_loop] != NULL && fun_suites[i_loop]() != 0)
        {
            CU_cleanup_registry();
            return CU_get_error();
        }
    }

    apmtest_cunit_unregistry();

    fr_slab_destroy();

    return 0;
}
